import 'package:flutter/material.dart';
import 'package:jd_flutter/base/utils/jd_log.dart';
import 'package:jd_flutter/base/widget/basic/base_gesture.dart';
import 'package:jd_flutter/modules/pages/home/models/home_model.dart';

class TextMoreWidget extends StatefulWidget {
  final HomeModel? homeModel;
  const TextMoreWidget({super.key, this.homeModel});

  @override
  State<TextMoreWidget> createState() => _TextMoreWidgetState();
}

class _TextMoreWidgetState extends State<TextMoreWidget> {
  int maxLines = 3;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(22, 0, 22, 0),
      child: LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
        String text = widget.homeModel?.showText ?? '';
        TextStyle textStyle = const TextStyle(
            color: Color(0xff222222),
            fontSize: 13,
            fontWeight: FontWeight.bold);
        final textSpan = TextSpan(text: text, style: textStyle);
        final textPainter = TextPainter(
            text: textSpan,
            maxLines: maxLines,
            textDirection: TextDirection.ltr);
        textPainter.layout(maxWidth: constraints.maxWidth);
        final lineCount = textPainter.computeLineMetrics().length;
        jdLog('lineCount---$lineCount');
        return Stack(alignment: Alignment.bottomRight, children: [
          Text(text,
              style: textStyle,
              maxLines: maxLines,
              overflow: TextOverflow.ellipsis),
          Positioned(
              bottom: 1,
              child: Visibility(
                  visible: textPainter.didExceedMaxLines || lineCount > 3,
                  child: Row(
                    children: [
                      Container(
                        alignment: Alignment.center,
                        decoration: const BoxDecoration(
                          color: Colors.white,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            CommonGestureDetector(
                                onTap: () {
                                  if (maxLines == 3) {
                                    maxLines = 100;
                                  } else {
                                    maxLines = 3;
                                  }
                                  setState(() {});
                                },
                                child: maxLines == 3
                                    ? const Text.rich(
                                        TextSpan(text: '...', children: [
                                          TextSpan(
                                              text: '更多',
                                              style: TextStyle(
                                                  color: Colors.blue,
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.bold)),
                                        ]),
                                        style: TextStyle(
                                            color: Colors.black,
                                            fontSize: 12,
                                            fontWeight: FontWeight.bold),
                                      )
                                    : const Text('收起',
                                        style: TextStyle(
                                            color: Colors.blue,
                                            fontSize: 12,
                                            fontWeight: FontWeight.bold))),
                          ],
                        ),
                      ),
                    ],
                  )))
        ]);
      }),
    );
  }
}
